package Review2;

public class Test06 {
	public static void main(String[] args) {
		int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
		int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
		System.out.println(constructBinaryTreeNode(pre, in).left.value);

	}

	public static BinaryTreeNode constructBinaryTreeNode(int[] pre,int[] in){
		if(pre==null||in==null||pre.length==0||in.length==0){
			return null;
		}
		
		return constructBinaryTreeNode(pre, 0,pre.length-1,in,0,in.length-1);
	}
	public static BinaryTreeNode constructBinaryTreeNode(int[]pre, int prestart,int preend,int[]in,int instart,int inend){
		if (prestart > preend || instart > inend) {
			return null;
		}
		BinaryTreeNode root = new BinaryTreeNode();
		root.value = pre[prestart];
		for(int i=instart;i<=inend;i++){
			if(in[i]==pre[prestart]){
				root.left=constructBinaryTreeNode(pre, prestart+1,i-instart+prestart,in,instart,i-1);
				root.right=constructBinaryTreeNode(pre, i-instart+prestart+1,preend,in,i+1,inend);
			}
		}
		return root;
	}

}
